package com.qzsoft.zhjd.server.modules.ludong.exp.controller;


import cn.hutool.core.convert.Convert;
import com.qzsoft.zhjd.server.common.utils.MapToBeanUtils;
import com.qzsoft.zhjd.server.common.utils.R;
import com.qzsoft.zhjd.server.modules.ludong.exp.entity.CollectExpDuctilityEntity;
import com.qzsoft.zhjd.server.modules.ludong.exp.entity.CollectLaboratoryDeviceEntity;
import com.qzsoft.zhjd.server.modules.ludong.exp.entity.CollectPlatformLaboratoryDataEntity;
import com.qzsoft.zhjd.server.modules.ludong.exp.service.CollectExpDuctilityService;
import com.qzsoft.zhjd.server.modules.ludong.exp.service.CollectExpSofteningPointService;
import com.qzsoft.zhjd.server.modules.ludong.exp.service.CollectLaboratoryDeviceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 *
 *功能描述：延度
 * @author jiangyafei
 * @email jiangyf_sd@163.com
 * @date 2020-06-09 11:17:30
 */
@Slf4j
@RestController
@RequestMapping("datacollect/exp/expductility")
public class CollectExpDuctilityController {
    @Autowired
    private CollectExpDuctilityService collectExpDuctilityService;
    @Autowired
    private CollectExpSofteningPointService collectExpSofteningPointService;
    @Autowired
    private CollectLaboratoryDeviceService deviceService;


    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody Map<String,Object> params){
        log.info("延度==="+params.toString());
        try{
            Map<String,Object> map = MapToBeanUtils.toReplaceKeyLow(params);
            CollectExpDuctilityEntity collectExpDuctilityEntity = MapToBeanUtils.mapToObject(map, CollectExpDuctilityEntity.class);
            if (StringUtils.isNotBlank(collectExpDuctilityEntity.getId())){
                collectExpDuctilityService.insertAndRemoveData(collectExpDuctilityEntity);
                CollectPlatformLaboratoryDataEntity convert = Convert.convert(CollectPlatformLaboratoryDataEntity.class, collectExpDuctilityEntity);
                collectExpDuctilityService.saveAsphalt(convert);
                return R.ok();
            }
            return R.error(400,"数据为空或id为空！");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return R.error(400,e.getMessage());
        }

    }

    @PostMapping("/ductilityInsert")
    public R ductilityInsert(@RequestBody Map<String,Object> params) throws Exception{
        CollectPlatformLaboratoryDataEntity collectPlatformLaboratoryDataEntity = MapToBeanUtils.mapToCapitalObject(params, CollectPlatformLaboratoryDataEntity.class);
        collectPlatformLaboratoryDataEntity.setTestType("YD");
        collectPlatformLaboratoryDataEntity.setEquipmentNumber(params.get("DEVICENO").toString());
        //查询设备编号
        QueryWrapper<CollectLaboratoryDeviceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code",params.get("DEVICENO").toString());
        List<CollectLaboratoryDeviceEntity> list = deviceService.list(queryWrapper);
        if (list.size()>0) {
            collectPlatformLaboratoryDataEntity.setDeviceId(params.get("DEVICENO").toString());
            collectPlatformLaboratoryDataEntity.setDeptId(list.get(0).getBaseCode());
            collectPlatformLaboratoryDataEntity.setDeptPkid(list.get(0).getDeptPkid());
        }
        collectPlatformLaboratoryDataEntity.setUploadTime(LocalDateTime.now());

        collectExpDuctilityService.saveAsphalt(collectPlatformLaboratoryDataEntity);

        return R.ok();
    }


}
